Cellen 2

R basics II — vectors

Gavin Simpson

Aarhus University

2025-02-12

Vectors

Vectors

Vectors are the fundamental way that data are stored in R

R doesn’t have scalars — single values — just vectors

Vectors are a one-dimensional collection of values in a single unit

(But see lists later in the course)

Atomic vectors are vectors whose elements are all of the same type

Creating vectors

Create vectors with c() (for combine)

numbers <- c(1, 4, 6, 10)
numbers
## [1]  1  4  6 10
people <- c("Alice", "Bob", "Claire", "David")
people
## [1] "Alice"  "Bob"    "Claire" "David"

Number of elements via length()

length(people)
## [1] 4

Creating vectors

Many other ways: seq(), rep()

seq(1, 5)
## [1] 1 2 3 4 5
seq(1, 10, by = 2)
## [1] 1 3 5 7 9
seq(1, 2, length = 5L)
## [1] 1.00 1.25 1.50 1.75 2.00
rep(c(1,2), each = 2)
## [1] 1 1 2 2
rep(c(1,2), times = 2)
## [1] 1 2 1 2

Vectorized operations

Vectors are a power feature of R as they allow us to write more expressive code

v1 <- c(3, 1, 4, 1, 5)
v2 <- c(1, 6, 1, 8, 0)
v1 + v2
## [1] 4 7 5 9 5

In other languages, to achieve this you might have to loop (iterate) over the indices of the vectors to add each pair of elements in turn

We’ll won’t talk more about loops and iteration but you can read about them in R4DS if you want

Recycling

What if we have vectors of different lengths?

v1 <- c(1, 3, 5, 1, 5)
v2 <- c(1, 2)
v1 + v2
## Warning in v1 + v2: longer object length is not a multiple of shorter object
## length
## [1] 2 5 6 3 6

v2 is recycled until it is of the correct length

Dangerous & powerful — best avoided

Working with data frames helps avoid this

Recycling

Vectorized functions

Most functions in R accept vectors as inputs

v1 <- c(10, 5, 2, 4)
sum(v1)
## [1] 21
prod(v1)
## [1] 400
length(v1)
## [1] 4
round(v1 + runif(length(v1)), 2)
## [1] 10.44  5.67  2.23  4.89

Indexing vectors

Having stored data in a vector we might want to access certain elements of the vector

Use [ plus a vector of indices to access elements of a vector

v1
## [1] 10  5  2  4

Can also use negative indices to exclude those elements

v1[1]
## [1] 10
v1[4]
## [1] 4
v1[length(v1)]
## [1] 4
v1[2:3]
## [1] 5 2
v1[-c(1,3)]
## [1] 5 4

Indexing vectors

If we give the elements of the vector names we can index using those

names(v1) <- people
v1
##  Alice    Bob Claire  David 
##     10      5      2      4
v1["Alice"]
## Alice 
##    10
people[2]
## [1] "Bob"
v1[people[2]]
## Bob 
##   5

Indexing vectors

We can also use a logical vector to select (TRUE) or exclude (FALSE) elements

v1
##  Alice    Bob Claire  David 
##     10      5      2      4
filt <- rep(c(TRUE, FALSE), each = 2)
filt
## [1]  TRUE  TRUE FALSE FALSE
v1[filt]
## Alice   Bob 
##    10     5
v1[!filt]
## Claire  David 
##      2      4

Indexing vectors

Any expression that evaluates to

  • numeric (possibly negative)
  • character (assuming named)
  • logical

can be used to index a vector

v1
##  Alice    Bob Claire  David 
##     10      5      2      4

Can also assign new values to elements

v1[4] <- 15
v1
##  Alice    Bob Claire  David 
##     10      5      2     15
v1 < 10
##  Alice    Bob Claire  David 
##  FALSE   TRUE   TRUE  FALSE
v1[v1 < 10]
##    Bob Claire 
##      5      2